package iks.utils.ui 
{
	import flash.display.*;
	import flash.events.*;
	import flash.media.*;
	import iks.iface.*;
	import iks.sound.*;
	import iks.utils.*;
	
	/**
	 * 位图构造的按钮类
	 * @author StefanieKaka
	 */
	public class BMPButton extends Sprite
	{
		/// 按钮单击事件
		public static const CLICK : String = "BMPButtonClick";
		
		/// 构造函数
		public function BMPButton(out : Bitmap, over : Bitmap, 
							overSound : KSound = null, clickSound : KSound = null) 
		{
			if (out == over)
				throw new Error("移除位图与移入位图不能是同一个对象");
			
			m_bmpOut = out;
			m_bmpOver = over;
			this.addChild(m_bmpOut);
			this.addChild(m_bmpOver);
			m_bmpOver.visible = false;
			
			m_overSound = overSound;
			m_clickSound = clickSound;
			
			this.addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler);
			this.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler);
			this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
			this.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
		}
		
///////////////////////////////////////////////////////////////////////////////////////////////////
// 公共域
///////////////////////////////////////////////////////////////////////////////////////////////////
		
		/// 播放按下事件
		public function get responseClick() : Boolean
		{
			return m_responseClick;
		}
		
		public function set responseClick(value : Boolean) : void
		{
			m_responseClick = value;
		}

///////////////////////////////////////////////////////////////////////////////////////////////////
// 私有域
///////////////////////////////////////////////////////////////////////////////////////////////////
		
		/// 鼠标移开事件
		private function onRollOutHandler(evt : MouseEvent) : void
		{
			if (!this.mouseEnabled)
				return;
			
			m_activeCnt = 0;
			m_bmpOut.visible = true;
			m_bmpOver.visible = false;
		}
		
		/// 鼠标移动到实例上
		private function onRollOverHandler(evt : MouseEvent) : void
		{
			if (!this.mouseEnabled)
				return;
			
			if (m_overSound != null)
				m_overSound.play();
			
			++m_activeCnt;
			
			m_bmpOut.visible = false;
			
			m_bmpOver.x = 0;
			m_bmpOver.y = 0;
			m_bmpOver.visible = true;
		}
		
		/// 鼠标按下事件
		private function onMouseDownHandler(evt : MouseEvent) : void
		{
			if (!this.mouseEnabled)
				return;
			
			if (m_responseClick)
			{
				++m_activeCnt;
				m_bmpOver.x += 1;
				m_bmpOver.y += 1;
			}
		}
		
		/// 鼠标抬起事件
		private function onMouseUpHandler(evt : MouseEvent) : void
		{
			if (!this.mouseEnabled)
				return;
			
			m_bmpOver.x = 0;
			m_bmpOver.y = 0;
			
			if (m_activeCnt >= 2)
			{
				if (m_clickSound != null)
					m_clickSound.play();
				
				this.dispatchEvent(new Event(CLICK));
			}
		}
		
		private var m_activeCnt		: int = 0;
		private var m_responseClick	: Boolean = true;
		private var m_overSound		: KSound = null;
		private var m_clickSound	: KSound = null;
		protected var m_bmpOut		: Bitmap = null;
		protected var m_bmpOver		: Bitmap = null;
	}

}

































///////////////////////////////////////////////////////////////////////////////////////////////////
// end file